package com.dimanche.library_db.repository;

import androidx.core.app.NavUtils;
import androidx.lifecycle.LiveData;

import com.dimanche.library_db.AppDatabase;
import com.dimanche.library_db.entity.UserEntity;
import com.dimanche.library_db.entity.WordEntity;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * author : Dimanche
 * date   : 2021/3/22 10:42
 * desc   :字典相关数据仓库
 * version: 1.0
 */
public class DirRepository {

    private volatile static DirRepository INSTANCE;

    public static DirRepository getInstance() {
        if (INSTANCE == null) {
            synchronized (DirRepository.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DirRepository();
                }
            }
        }
        return INSTANCE;
    }

    //=======================插入================
    public Observable<Long> insert(WordEntity t) {
        return Observable.create((ObservableOnSubscribe<Long>) emitter -> {
            Long insert = AppDatabase.getDatabase().getDirDao().insert(t);
            emitter.onNext(insert);
            emitter.onComplete();
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<List<Long>> insert(WordEntity... ts) {
        return Observable.create((ObservableOnSubscribe<List<Long>>) emitter -> {
            List<Long> insert = AppDatabase.getDatabase().getDirDao().insert(ts);
            emitter.onNext(insert);
            emitter.onComplete();
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    public Observable<List<Long>> insert(List<WordEntity> ts) {
        return Observable.create((ObservableOnSubscribe<List<Long>>) emitter -> {
            List<Long> insert = AppDatabase.getDatabase().getDirDao().insert(ts);
            emitter.onNext(insert);
            emitter.onComplete();
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());

    }


    //==========================查询=======================
    public LiveData<List<WordEntity>> queryAll() {
        return AppDatabase.getDatabase().getDirDao().queryAll();
    }

    public LiveData<WordEntity> queryById(int id) {
        return AppDatabase.getDatabase().getDirDao().queryById(id);
    }

    public LiveData<List<WordEntity>> queryByWord(String word) {
        return AppDatabase.getDatabase().getDirDao().queryByWord(word);
    }


}
